/**
 * 
 */
package edu.umd.clip.lm.questions;

import org.w3c.dom.*;

import edu.umd.clip.lm.model.AbstractProbTree;
import edu.umd.clip.lm.model.data.Context;
import edu.umd.clip.lm.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class NotQuestion extends Question {
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#getQuestionType()
	 */
	@Override
	public int getQuestionType() {
		return NOT_QUESTION;
	}
	private Question question;

	public NotQuestion(Question q) {
		super(q.getTimeOffset(), q.isAboutHidden());
		question = q;
	}
	/**
	 * @param xmlnode
	 */
	public NotQuestion(Element xmlnode) {
		super(xmlnode);
		question = Question.fromXML(XMLUtils.getFirstElementByTagName(xmlnode, Question.XML_ELEMENT_NAME));
	}

	public static Question fromXML(Element xmlnode) {
		return new NotQuestion(xmlnode);
	}
	
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#implication(edu.umd.clip.lm.questions.Question, boolean)
	 */
	@Override
	public Boolean implication(Question q, boolean value) {
		Boolean result = q.implication(q, value);
		if (result == null) return null;
		return !result.booleanValue();
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#test(edu.umd.clip.lm.model.training.Context)
	 */
	@Override
	public boolean test(Context ctx) {
		return !question.test(ctx);
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#test(java.util.AbstractList)
	 */
	@Override
	public TestResult test(AbstractProbTree[] context) {
		TestResult result = question.test(context);
		result.setResult(!result.getResult());
		return result;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#test(java.util.AbstractList)
	 */
	@Override
	public TestResult test(AbstractProbTree item) {
		TestResult result = question.test(item);
		result.setResult(!result.getResult());
		return result;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#createXML(org.w3c.dom.Document)
	 */
	@Override
	public Element createXML(Document doc) {
		Element elem = super.createXML(doc);
		elem.appendChild(question.createXML(doc));
		return elem;
	}
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.questions.Question#toShortString()
	 */
	@Override
	public String toShortString() {
		return "[NOT " + question.toShortString() + ']';
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "[NOT " + question.toString() + ']';
	}

}
